<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="100px">
          <el-row :gutter="10" style="text-align: center">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户名称">
                <zSelect :value.sync="query.customerId" url="api/dropDown/basCustomerList" style="width:  100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户业务员">
                <zSelect :value.sync="query.userId" url="api/dropDown/sysUserList" style="width:  100%;"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="销售区域">
                <treeselect v-model="query.customerAreaId" :options="areaData" :normalizer="normalizer" placeholder="请选择"/>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="是否会员">
                <el-select
                  v-model="query.isVip"
                  class="filter-item"
                  clearable
                  placeholder="请选择"
                  size="small"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in dict.common_yes_or_no"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="电话" prop="code">
                <el-input v-model="query.userPhone" />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <crudOperation :permission="permission" />

      <!--表格渲染-->
      <el-table ref="table"  border
                height="700"
                show-summary
                @cell-dblclick="row=>crud.toEdit(row)"
                v-loading="crud.loading"
                :data="crud.data"
                size="small"
                style="width: 100%;"
                :summary-method="crud.sumRow"
                @row-click="crud.selectRow"
                :row-style="crud.rowClass"
                :row-class-name="crud.tableRowClassName"
                @selection-change="crud.selectionChangeHandler"
                @row-dblclick="crud.toDetailPlus('financeCustomerBalanceDetail',$event.customerId)">
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60"/>
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerCode')" prop="customerCode" label="客户编码"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerName')" prop="customerName" label="客户名称"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userName')" prop="userName" label="业务员"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('customerAreaName')" prop="customerAreaName" label="区域"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('isVip')" prop="isVip" label="是否会员">
          <template slot-scope="scope">
            {{ dict.label.common_yes_or_no[scope.row.isVip] }}
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('balanceAmount')" prop="balanceAmount" label="余额"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deductionAmount')" prop="deductionAmount" label="抵扣券"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userBirthday')" prop="userBirthday" label="生日"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userPhone')" prop="userPhone" label="电话"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('discountAmount')" prop="discountAmount" label="折扣池"/>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudFinanceCustomerBalance from '@/api/erp/financeCustomerBalance';
import CRUD, {crud, form, header, presenter} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import {getAreaTree} from "@/api/erp/basCustomerArea";

// crud交由presenter持有
const defaultCrud = CRUD({
  title: '客户余额',
  url: 'api/financeCustomerBalance',
  sort: 'id,desc',
  optShow: {
    add: false,
    edit: false,
    del: false,
    download: true,
    leadingIn: false,
    enable: false,
    disable: false,
    pass: false,
    reject: false,
    invalid: false,
    recover: false,
    downloadDetail: false,
    print: false,
    printDetail: false
  },
  crudMethod: {...crudFinanceCustomerBalance}
})
const defaultForm = {
  id: null,
  customerId: null,
  customerCode: null,
  customerName: null,
  isContact: null,
  customerAreaId: null,
  customerAreaCode: null,
  customerAreaName: null,
  customerTypeId: null,
  customerTypeCode: null,
  customerTypeName: null,
  pCustomerId: null,
  pCustomerCode: null,
  pCustomerName: null,
  mCustomerId: null,
  mCustomerCode: null,
  mCustomerName: null,
  sCustomerId: null,
  sCustomerCode: null,
  sCustomerName: null,
  deptId: null,
  deptCode: null,
  deptName: null,
  userId: null,
  userCode: null,
  userName: null,
  balanceAmount: null,
  seq: null,
  remark: null,
  createUserId: null,
  createUserCode: null,
  createUserName: null,
  createTime: null,
  updateUserId: null,
  updateUserCode: null,
  updateUserName: null,
  updateTime: null
}
export default {
  name: 'FinanceCustomerBalance',
  components: {pagination, crudOperation, rrOperation, udOperation, Treeselect},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: [
    "common_yes_or_no",
  ],
  data() {
    return {
      permission: {
        add: ['admin', 'financeCustomerBalance:add'],
        edit: ['admin', 'financeCustomerBalance:edit'],
        del: ['admin', 'financeCustomerBalance:del'],
        import: ['admin', 'financeCustomerBalance:import'],
        export: ['admin', 'financeCustomerBalance:export'],
        copy: ['admin', 'financeCustomerBalance:copy'],
        upload: ['admin', 'financeCustomerBalance:upload'],
        download: ['admin', 'financeCustomerBalance:download'],
      },
      rules: {},
      areaData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  watch: {},
  updated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })},
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    [CRUD.HOOK.afterRefresh](crud, form) {
      console.log('crud, form',crud, form)
    },
    getDepts() {
      getAreaTree().then(res => {
        this.areaData = res
      })
    },
    checkArea(data, node) {
      this.query.customerAreaId = data.id
    },
    normalizer(node) {
      //去掉children=[]的children属性
      if (node.children == null || node.children.length == 0) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    }
  },
  created() {
    this.getDepts()
  },
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}

.search-select {
  width: 100%;
}
</style>
